class Solution {

public:
    int jump(vector<int>& nums) {
        int left = 0, right = 0, maxPos = 0, ret = 0, n = nums.size();
        // 这一次起跳的左端点，右端点，下一次起跳的右端点
        while(left <= right)
        {
            if(maxPos >= n - 1)
                return ret;
            for(int i = left; i <= right; ++i)
            {
                maxPos = max(maxPos, i + nums[i]);
            }
            left = right + 1; // 更新起跳左右端点并++ret
            right = maxPos;
            ++ret;
        }
        return -1; // 不会走到这
    }
};
/*
class Solution {
public:
    int jump(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp(n, INT_MAX);
        dp[0] = 0;
        for(int i = 1; i < n; ++i)
        {
            for(int j = 0; j < i; ++j)
            {
                if(nums[j] + j >= i) 
                {
                    dp[i] = dp[j] + 1;
                    break;
                }
            }
        }
        return dp[n -1];
    }
};
*/